//
//  ShellModifyPersonInfoViewController.m
//  ShellWallert
//
//  Created by lvzhao on 2019/9/26.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellModifyPersonInfoViewController.h"

@interface ShellModifyPersonInfoViewController ()

@end



@implementation ShellModifyPersonInfoViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI{
   
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 20, K_SCREENWIDTH - 30, 50)];
    textField.placeholder = LZLocalizedString(@"请输入昵称");
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.text = self.nickName;
    textField.cornerRadius = 6;
    textField.bColor = UIColorHex(0x11385E);
    textField.bWidth = 1;
    textField.textColor = UIColorHex(0xFFFFFF);
    textField.backgroundColor = COLOR_TINT;
    [self.view addSubview:textField];
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:textField.placeholder attributes:@{NSForegroundColorAttributeName: UIColorHex(0x678DA8)}];

    UIView *leftView = [UIView new];
    leftView.frame = CGRectMake(0, 0, 17, 50);
    textField.leftView = leftView;
    //设置显示模式为永远显示(默认不显示)
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    
    //完成
    UIButton *finshBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"完成") titleColor:UIColorHex(0xFFFFFF) fontSize:14];
    [self initBarItem:finshBtn withType:1];
    
    [[textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        //过滤表情符
        if ([self stringContainsEmoji:textField.text] || [self hasEmoji:textField.text]) {
            textField.text = [self disableEmoji:textField.text];
        }
        textField.text = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        if(textField.text.length == 0){
            finshBtn.enabled = NO;
            finshBtn.alpha = 0.5;
        } else {
            finshBtn.enabled = YES;
            finshBtn.alpha = 1;

        }
        
    }];
    @weakify(self);
    [[finshBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self submitNickName:textField.text];
    }];
}

//提交m昵称
- (void)submitNickName:(NSString *)name{
    
    [LZNetworkingManager lz_request:@"post" url:kSaveName params:@{@"name":name?:@""} success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            ShellUserDetailModel *infoModel = [LZToolCache getUserInfo];
            infoModel.name = name;
            [LZToolCache saveUserInfo:infoModel];
            showDelayedDismissTitle(LZLocalizedString(@"修改成功"), nil);
            
            [LZTool.currentViewController.navigationController popViewControllerAnimated:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:k_APPUserInfoWillChangeNotificationName object:nil];
        }
    } failure:^(NSError * _Nullable error) {}isLoading:YES isFailTip:YES];
    
    
    
    
    
    
}


#pragma mark - custool
//判断是否含有表情符号 YES-有 NO-没有
- (BOOL)stringContainsEmoji:(NSString *)string {
    __block BOOL returnValue =NO;
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        const unichar hs = [substring characterAtIndex:0];
        if (0xd800) {
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (substring.length > 1) {
                    const unichar ls = [substring characterAtIndex:1];
                    const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        returnValue =YES;
                    }
                }
            }else if (0x2100 <= hs && hs <= 0x27ff){
                returnValue =YES;
            }else if (0x2B05 <= hs && hs <= 0x2b07) {
                returnValue =YES;
            }else if (0x2934 <= hs && hs <= 0x2935) {
                returnValue =YES;
            }else if (0x3297 <= hs && hs <= 0x3299) {
                returnValue =YES;
            }else{
                if (substring.length > 1) {
                    const unichar ls = [substring characterAtIndex:1];
                    if (ls == 0x20e3) {
                        returnValue =YES;
                    }
                }
            }
            if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50 || hs == 0xd83e) {
                returnValue =YES;
            }
            
        }
    }];
    return returnValue;
}

//判断第三方键盘中的表情
- (BOOL)hasEmoji:(NSString*)string {
    NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}
//去除表情
- (NSString *)disableEmoji:(NSString *)text {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, [text length]) withTemplate:@""];
    return modifiedString;
}


@end
